home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
CD ROM Paradise Collection 4
/
CD ROM Paradise Collection 4 1995 Nov.iso
/
basic
/
pbasmlib.zip
/
ARRAY.DOC
< prev
next >
Wrap
Text File
|
1994-02-12
|
13KB
|
269 lines
PBASMLIB Assembly Language Routines for PB3C
Version 1.0
Integer/Word Array Manipulation Routines (ARRAY)
(C) Copyright 1994 by Tim Gerchmez
All Rights Reserved.
This unit contains many routines that manipulate numeric (integer and
word) arrays in various ways, all of them lightning-quick in assembly
language. It is contained in PBASMLIB.PBL, and the routines listed here
will be immediately accessible using $INCLUDE "PBASMLIB.INC" and
$LINK "PBASMLIB.PBL" at the top of your programs.
CAUTION: These routines do not do error checking. Be sure to pass
them only valid values, or sections of memory may be unintentionally
overwritten. For example, calling FILLARRAY with a 5-element integer
array, and asking it to fill 10 elements starting with the first, will
cause problems, and may result in a system crash.
The ARRAY module supports both integer and word arrays. Integer arrays
elements have a value from -32768 to +32767 (I.E. they are signed), while
word array elements have a value ranging from 0 to 65535 (unsigned). Only
single-dimensional arrays are supported by this module, and an array can be
dimensioned to only integer values (I.E. the maximum LBOUND is -32768 and the
maximum UBOUND is 32767). This allows you to use arrays that are up to 64K
elements in length. For example, the following BASIC code is valid:
dim ary%(-32768 to 32767)
call fillarray(ary%(-32768,65535,10) 'Fills elements -32768 to 32767 with 10
The formula for calculating the highest element filled given the parameters most
of these commands use is (start element + number of elements) - 1. For example,
if you use [FILLARRAY AR%(-11),80,10], calculate the top element by using
(-11 + 80) - 1, which means that element 68 will be the highest element filled.
Note: a "bug" in PowerBASIC prevents you from accessing the entire contents of
an array using a for/next loop with an integer counter variable. For example,
the following code will cause an OVERFLOW error:
dim ary%(-32768 to 32767)
for t% = -32768 to 32767 : print ary%(t%) : next t%
This is because the last iteration of the loop will increase the loop variable
one number beyond the upper limit, causing an error condition. For this reason,
it's recommended that you use 32766 for a UBOUND limit with arrays rather than
the value 32767.
================================================================================
sub negarray(ary%(se%),ne??)
Negates the values of an integer array, making negative
numbers positive and positive numbers negative. Starts with
a particular element and continues for a particular number of
elements. This command applies to integer arrays only, as negation
is a moot point with word arrays.
ary%(se%): Set se% to the starting element you want to negate in ary%.
Only integer arrays can be passed to this routine.
ne??: Set to the number of elements to negate, starting with and
including the number specified with se%.
Example: negarray ary%(1),50 'Negate elements 1 through 50
Example: negarray ary%(-80),41 'Negate elements -80 through -40
================================================================================
sub shrarray(ary(se%),ne??,ct%)
Shifts the bits of each element in an integer or
word array right (SHR) by a certain number of bits.
This effectively divides each number by 2 times
the number of bits shifted [ary%(se%)\(ct% * 2)].
ary(se%): Set se% to the start element of the array to SHR. Either
integer or word arrays can be passed.
ne??: Set to number of elements to SHR, inclusive of the
first element specified in se%.
ct%: Set to number of bits to shift right by
Example: shrarray ary%(1),100,1 'Shifts elements 1 through 100 right by one bit
Example: shrarray ary%(-5),11,4 'Shifts elements -5 through 5 right by 4 bits
Example: shrarray ary??(1),100,2 'Shifts elements 1 through 100 right by 2 bits
================================================================================
sub shlarray(ary(se%),ne??,ct%)
Shifts the bits of each element in an integer or
word array left (SHL) by a certain number of bits.
This effectively multiplies each number by 2 times
the number of bits shifted [ary%(se%)*(ct% * 2)].
ary(se%): Set se% to the start element of the array to SHL. Either
integer or word arrays can be passed.
ne??: Set to number of elements to SHL, inclusive of the
first element specified in se%.
ct%: Set to number of bits to shift left by
Example: shlarray ary%(1),100,1 'Shifts elements 1 through 100 left by one bit
Example: shlarray ary%(-5),11,4 'Shifts elements -5 through 5 left by 4 bits
Example: shlarray ary??(1),100,2 'Shifts elements 1 through 100 left by 2 bits
================================================================================
sub rorarray(ary(se%),ne??,ct%)
Rotates the bits of each element in an integer or
word array right (ROR) by a certain number of bits.
ary(se%): Set se% to the start element of the array to ROR. Either
integer or word arrays can be passed.
ne??: Set to number of elements to ROR, inclusive of the
first element specified in se%.
ct%: Set to number of bits to rotate right by
Example: rorarray ary%(1),100,1 'Rotates elements 1 through 100 right by one bit
Example: rorarray ary%(-5),11,4 'Rotates elements -5 through 5 right by 4 bits
Example: rorarray ary??(1),100,2 'Rotates elements 1 through 100 right by 2 bits
================================================================================
sub rolarray(ary(se%),ne??,ct%)
Rotates the bits of each element in an integer or
word array left (ROL) by a certain number of bits.
ary(se%): Set se% to the start element of the array to ROL. Either
integer or word arrays can be passed.
ne??: Set to number of elements to ROL, inclusive of the
first element specified in se%.
ct%: Set to number of bits to rotate left by
Example: rolarray ary%(1),100,1 'Rotates elements 1 through 100 left by one bit
Example: rolarray ary%(-5),11,4 'Rotates elements -5 through 5 left by 4 bits
Example: rolarray ary??(1),100,2 'Rotates elements 1 through 100 left by 2 bits
================================================================================
sub notarray(ary(se%),ne??)
Logically NOTs the bits of each element in an integer or word array
with a given value, starting with a particular element and continuing
for a particular number of elements. This forms the one's complement
(inverts the bits) of the array elements.
ary(se%): Set se% to the start element of the array to NOT. Either
integer or word arrays can be passed.
ne??: Set to number of elements to NOT, inclusive of the
first element specified in se%.
Example: notarray ary%(1),100 'NOTs elements 1 through 100
Example: notarray ary%(-5),11 'NOT elements -5 through 5
Example: notarray ary??(1),100 'NOT elements 1 through 100
================================================================================
sub xorarray(ary(se%),ne??,xv??)
Exclusive-OR's (XOR's) the values of each element in an integer or
word array with a given value, starting with a particular element
and continuing for a particular number of elements.
ary(se%): Set se% to the start element of the array to XOR. Either
integer or word arrays can be passed.
ne??: Set to number of elements to XOR, inclusive of the
first element specified in se%.
av??: Set to the word value (0-65535) to XOR each array element
with (see examples below).
Example: xorarray ary%(1),100,32 'XORs elements 1 through 100 with 32,
'toggling bit 5.
Example: xorarray ary%(-5),11,40 'XOR elements -5 through 5 with 40,
'toggling bits 5 and 3.
Example: xorarray ary??(1),100,65535 'XOR elements 1 through 100 with 65535,
'toggling all bits.
================================================================================
sub orarray(ary(se%),ne??,ov??)
Logically ORs the values of each element in an integer or word
array with a given value, starting with a particular element and
continuing for a particular number of elements.
ary(se%): Set se% to the start element of the array to OR. Either
integer or word arrays can be passed.
ne??: Set to number of elements to OR, inclusive of the
first element specified in se%.
av??: Set to the word value (0-65535) to OR each array element
with (see examples below).
Example: orarray ary%(1),100,32 'ORs elements 1 through 100 with 32,
'switching on bit 5 if not already set.
Example: orarray ary%(-5),11,40 'OR elements -5 through 5 with 40,
'switching on bits 5 and 3.
Example: orarray ary??(1),100,8192 'OR elements 1 through 100 with 8192.
================================================================================
sub andarray(ary(se%),ne??,av??)
Logically ANDs the values of each element in an integer or word
array with a given value, starting with a particular element and
continuing for a particular number of elements.
ary(se%): Set se% to the start element of the array to AND. Either
integer or word arrays can be passed.
ne??: Set to number of elements to AND, inclusive of the
first element specified in se%.
av??: Set to the word value (0-65535) to AND each array element
with (see examples below).
Example: andarray ary%(1),100,31 'AND elements 1 through 100 with 31,
'limiting to values between 0 and 31.
Example: andarray ary%(-5),11,40 'AND elements -5 through 5 with 40.
Example: andarray ary??(1),100,8192 'AND elements 1 through 100 with 8192.
================================================================================
sub addarray(ary(se%),ne??,va%)
Adds a given value to each element of an integer or word array,
starting with a particular element and continuing for a particular
number of elements.
ary(se%): Set se% to the start element of the array to add the
value to. Either integer or word arrays can be passed.
ne??: Set to number of elements to add the value to, inclusive
of the first element specified in se%.
va%: Set to the integer value to add to the specified array elements.
To subtract, add a negative value. If the calculated new value
exceeds the limits of an integer/word value, it will "wrap around"
rather than causing an error. To add a word-length value to a word
array, use the BITS% statement (see last example below).
Example: addarray ary%(1),100,32 'Add 32 to elements 1 through 100 of ary%().
Example: addarray ary%(-5),11,40 'Adds 40 to elements -5 through 5 of ary%().
Example: addarray ary??(1),100,-32 'Subtract 32 from elements 1 through 100 of ary??().
Example: addarray ary??(1),10,bits%(40000) 'Add 40000 to elements 1 through 10 of ary??().
================================================================================
sub randfillarray(ary(se%),ne??)
Fills (initializes) an integer or word array with
pseudo-random values (-32768 to 32767 for INT arrays
or 0 to 65535 for word arrays.) Use ANDARRAY to limit
the array to certain ranges of values.
ary(se%): Set to array to fill, with se% equalling the starting
(first) element to fill. Integer or word arrays can
be passed.
ne??: Set to number of elements to fill, including the first
element (se%). See example below.
Example: randfillarray ary%(1),100 'Fills elements 1 through 100 with random values
Example: randfillarray ary%(-10),20 'Fills elements -10 through 9 with random values
Example: randfillarray ary??(5),10 'Fills elements 5 through 15 with random values
================================================================================
sub fillarray(ary(se%),ne??,v%)
Fills (initializes) an integer or word array with a
certain specific value.
ary(se%): Set to array to fill, with se% equalling the starting
(first) element to fill. Integer or word arrays can
be passed.
ne??: Set to number of elements to fill, including the first
element (se%). See example below.
v%: Set to value to fill array with (integer only). To
convert a word value, use the BITS% command.
Example: fillarray ary%(1),100,-10 'Fills elements 1 through 100 with -10.
Example: fillarray ary%(-10),20,50 'Fills elements -10 through 9 with 50.
Example: fillarray ary??(5),10,32 'Fills elements 5 through 15 with 32.
Example: fillarray ary??(1),100,bits%(65000) 'Fills elements 1 through 100
'with the value 65000.